package com.test.funcitons.example02;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.function.BiFunction;
import java.util.function.Function;

public class FunctionCompositionWithGeneric {

    static BiFunction<String, Function<String, Animal>, Animal> GET_ANIMAL_NAME =
        (name, func) -> ObjectUtils.getIfNull(func.apply(name), Bird::new);

    static Function<String, Animal> GET_CAT  =  a -> new Cat();

    static Function<String, Animal> GET_BY_NAME  =  a -> {
        if (StringUtils.equalsIgnoreCase("dog", a)) {
            return new Dog();
        } else if(StringUtils.equalsIgnoreCase("cat", a)) {
            return new Cat();
        }
        return null;
    };

    public static void main(String[] args) {

        final Animal aa = GET_ANIMAL_NAME.apply("aa", GET_CAT);
        System.out.println(aa);

        final Animal bb = GET_ANIMAL_NAME.apply("aa", GET_BY_NAME);
        System.out.println(bb);
    }
}
